"""
Case Type   : 服务端工具
Case Name   : 验证gs_ssh工具功能
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1.显示帮助信息
    2.显示版本信息
    3.gs_ssh -c执行命令
Expect      :
    1.显示正确
    2.显示正确
    3.成功
History     :
"""
import unittest
import os
from yat.test import Node
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger



class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_node = Node('dbuser')
        self.constant = Constant()

    def test_server_tools1(self):
        self.log.info('------------------显示帮助信息------------------')
        cmd_list = ['-?', '--help']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                gs_ssh {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)

            self.log.info('-------------截取Usage:到General options部分的语法-------------------')
            start_index1 = msg1.find('Usage:') + len('Usage:')
            start_index2 = msg1.find('General options:')
            temp = msg1[start_index1:start_index2].split('\n')
            options_list1 = []
            for j in temp[1:-2]:
                options_list1.append(j.strip())
            else:
                pass
            self.log.info(options_list1)
            grammar = [
                'gs_ssh -? | --help',
                'gs_ssh -V | --version',
                'gs_ssh -c COMMAND']
            if len(options_list1) == len(grammar):
                for opt in options_list1:
                    if opt in grammar:
                        self.log.info(f'{opt}---------语法校验通过---------')
                    else:
                        self.log.error(f'{opt}---------语法校验不通过---------')
            else:
                self.log.error('---------语法校验有误---------')

            self.log.info('--------------定义一个空的列表，将获取的参数写入这个列表，并打印出该列表-------------------')
            options_list2 = []
            for i in msg1[start_index2:].split('\n'):
                for j in i.split(' '):
                    if len(j) != 0:
                        if j[0] == '-':
                            options_list2.append(j)
                        else:
                            pass
                    else:
                        pass
            self.log.info(options_list2)
            parameter = ['-c', '-?,', '--help', '-V,', '--version']
            if len(options_list2) == len(parameter):
                for opt in options_list2:
                    if opt in parameter:
                        self.log.info(f'{opt}---------参数校验通过---------')
                    else:
                        self.log.error(f'{opt}---------参数校验不通过---------')
            else:
                self.log.error('---------参数校验有误---------')

        self.log.info('------------------显示版本号信息------------------')
        cmd_list = ['-V', '--version']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                                gs_ssh {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)
            self.assertIn('openGauss', msg1)

        self.log.info('---列出主备机当前目录下相关文件夹包含的文件---')
        list_file_cmd = f'source {macro.DB_ENV_PATH};' \
                        f'gs_ssh -c "pwd"'
        self.log.info(list_file_cmd)
        msg = self.primary_node.sh(list_file_cmd).result()
        self.log.info(msg)
        self.assertIn(self.constant.gs_ssh_success_msg, msg)

    def tearDown(self):
        self.log.info('--------------无需清理环境-------------------')
        self.log.info('------------------Opengauss_Function_Tools_gs_ssh_Case0001finish------------------')